using System;
using ClimbTracker.BusinessLogic.Entities;
using NHibernate;

namespace ClimbTracker.BusinessLogic.Dao
{
    /// <summary>
    /// Summary description for ClimbAttemptDao.
    /// </summary>
    public class ClimbAttemptDao : BaseDao
    {
        public ClimbAttemptDao()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        /// <summary>
        /// Retrieve a ClimbAttempt object from the database by its ID
        /// </summary>
        /// <param name="climbAttemptID"></param>
        /// <returns></returns>
        public ClimbAttempt retrieveClimbAttemptByID(int climbAttemptID)
        {
            ClimbAttempt climbAttempt = null;
            try
            {
                climbAttempt = (ClimbAttempt)currentSession.Load(typeof(ClimbAttempt), climbAttemptID);
            }
            catch (HibernateException ex)
            {
                Console.WriteLine("EXCEPTION: " + ex.Message);
            }

            return climbAttempt;

        }

        /// <summary>
        /// Create a new climb attempt entry in the database from the given ClimbAttempt object
        /// </summary>
        /// <param name="climbAttempt"></param>
        public void createClimbAttempt(ClimbAttempt climbAttempt)
        {
            ITransaction transaction = null;
            try
            {
                transaction = currentSession.BeginTransaction();

                currentSession.Save(climbAttempt);

                transaction.Commit();
            }
            catch (HibernateException ex)
            {
                if (transaction != null)
                    transaction.Rollback();
                Console.WriteLine("EXCEPTION: " + ex.Message);
            }
        }

        /// <summary>
        /// Delete a the database entry of the given ClimbAttempt object
        /// </summary>
        /// <param name="climbAttempt"></param>
        public void deleteClimbAttempt(ClimbAttempt climbAttempt)
        {
            ITransaction transaction = null;
            try
            {
                transaction = currentSession.BeginTransaction();

                currentSession.Delete(climbAttempt);

                transaction.Commit();
            }
            catch (HibernateException ex)
            {
                if (transaction != null)
                    transaction.Rollback();
                Console.WriteLine("EXCEPTION: " + ex.Message);
            }
        }

        /// <summary>
        /// Update the database entry of the given ClimbAttempt object
        /// </summary>
        /// <param name="climbAttempt"></param>
        public void updateClimbAttempt(ClimbAttempt climbAttempt)
        {
            ITransaction transaction = null;
            try
            {
                transaction = currentSession.BeginTransaction();

                currentSession.Update(climbAttempt);

                transaction.Commit();
            }
            catch (HibernateException ex)
            {
                if (transaction != null)
                    transaction.Rollback();
                Console.WriteLine("EXCEPTION: " + ex.Message);
            }
        }



    }
}
